			
			function price_to_eval(price) {
                price = price.replace('￥', '');
                price = price.replace('元', '');
                price = price.replace(' ', '');
                price = eval(price);
                return price;
            }

			// 当选定某种属性/规格的时候，更新价格显示
			function update_price_with_attr(){
				    var j = 0;	
					var attr_price;		
					var txt_id;	
					// 基础价格:
					var base_price = document.getElementById('goods_shop_price').value;
					var formBuy = document.forms['ECS_FORMBUY'];
					var elements = formBuy.elements;
					for(var i=0;i<elements.length;i++){
							//alert('item name = ' + formBuy.elements[i].name);
							var prefix = formBuy.elements[i].name.substr(0, 5);
							//alert('prefix=' + prefix);
							if (prefix == 'spec_' && 
										( formBuy.elements[i].type == 'radio' && formBuy.elements[i].checked) 
								)
							{
								txt_id = 'txt_'+formBuy.elements[i].id;
								attr_price = document.getElementById(txt_id).value;
								break;
							}						
					}
					attr_price = (attr_price == '')?'0':attr_price;
					attr_price=eval(base_price) + eval(attr_price);
					
					// 处理会员可以得到的积分：
					document.getElementById('goods_integral_points').innerHTML = attr_price;
					
					// 考虑数量
/*					var command_quantity = document.getElementById("command_quantity");			
					var iNumber = eval(command_quantity.value);
					attr_price = iNumber * attr_price;*/
					
					document.getElementById('product_price').innerHTML = '￥'+ attr_price;// + '元';
			}


            function update_package_price() {

                var i = 0;
                var shop_price = 0;
                var pack_price = 0;
                var total_shop_price = 0;
                var total_pack_price = 0;
                var total_goods_list = "";
                var bContinue = 1;
                var price_list = " ";
                //document.write("before while");
                var boxes = document.getElementsByName("package_boxes");

                var base_price = document.getElementById("product_price").innerHTML;

                base_price = price_to_eval(base_price);
                //total_goods_list = document.getElementById("product_main_goods_id").innerHTML;

                var itm_id = "";
				
				var iSelected =0;
				
                total_shop_price = eval(base_price);
                total_pack_price = eval(base_price);
                for (var i = 0; i < boxes.length; i++) {
                    var box = boxes.item(i);
                    if (box.checked) {
                        itm_id = box.id.split("_")[1];
                        //alert("item_id = " + itm_id);
						if(iSelected==0) {
							total_goods_list = itm_id;
							iSelected = 1;
						}else{
							total_goods_list = total_goods_list + "_" + itm_id;
						}
                        //alert("total_goods_list = " + total_goods_list);		
                        shop_price = document.getElementById("shop_" + box.id).innerHTML; // 原价					
                        shop_price = price_to_eval(shop_price);
                        total_shop_price = eval(total_shop_price) + eval(shop_price);

                        pack_price = document.getElementById("package_" + box.id).innerHTML; // 套餐价	
                        pack_price = price_to_eval(pack_price);
                        total_pack_price = eval(total_pack_price) + eval(pack_price);
                    }
                }
                //alert("total_shop_price = " + total_shop_price + "    total_pack_price = " + total_pack_price);
                document.getElementById("package_saving").innerHTML = eval(total_shop_price) - eval(total_pack_price);

                document.getElementById("package_shop_total_price").innerHTML = total_shop_price;

                document.getElementById("package_promotion_price").innerHTML = '￥' + total_pack_price;

                //document.getElementById("package_goods_list_for_cart").innerHTML = total_goods_list;
                //alert("goods list = " + total_goods_list);
				
				document.getElementById('pack_goods_id_list').value = total_goods_list;
            }

			//商品详情和农残检测
            function tab_show_product_detail() {
				var contentTab = document.getElementById("product_detail_tab");
				var srcTab = document.getElementById("product_desc");
				contentTab.innerHTML = srcTab.innerHTML;
				document.getElementById("product_tab_caption1").className = "product_details_btn_yes";
				document.getElementById("product_tab_caption2").className = "product_details_btn_no";				
            }

            function tab_show_product_pControl() {
				var contentTab = document.getElementById("product_detail_tab");
				var srcTab = document.getElementById("product_pControl");
				contentTab.innerHTML = srcTab.innerHTML;
				document.getElementById("product_tab_caption1").className = "product_details_btn_no";
				document.getElementById("product_tab_caption2").className = "product_details_btn_yes";					
            }
			
			function update_quantity(func){
				var command_quantity = document.getElementById("command_quantity");			
				var rawValue = command_quantity.value;
				if(isNaN(rawValue)) rawValue = 1;
				rawValue = Math.round(rawValue);
				if(rawValue <= 0 || rawValue > 999 ) rawValue = 1;
				var vValue = eval(rawValue);
				if(func=="add"){
					if(vValue < 100){
						vValue = vValue + 1	
					}
					command_quantity.value = vValue;
				}else{
					if(vValue > 1 ){
						vValue = vValue - 1	
					}
					command_quantity.value = vValue;				
				}
				//update_price_with_attr();		//如果需要价格考虑数量，那就加这一行
			}
			
			function check_cmd_quantity(){
				var command_quantity = document.getElementById("command_quantity");		
				//alert(command_quantity.value);	
				if(isNaN(command_quantity.value)){
					command_quantity.value = "1";					
				}else{
					if( command.quantity.value < 1){
						command_quantity.value = '1';
					}
					command_quantity.value = round(command_quantity.value);
					if(eval(command_quantity.value) < 0 || eval(command_quantity.value) > 100){
							command_quantity.value = "0";					
					}
				}
			}
			
			
			/* *
 * 添加商品到购物车 
 */
function addToCart(goodsId, parentId)
{
	
  var goods        = new Object();
  var spec_arr     = new Array();
  var fittings_arr = new Array();
  var number       = 1;
  var formBuy      = document.forms['ECS_FORMBUY'];
  var quick		   = 0;

  // 检查是否有商品规格 
  if (formBuy)
  {
    spec_arr = getSelectedAttributes(formBuy);

    if (formBuy.elements['command_quantity'])
    {
      number = formBuy.elements['command_quantity'].value;
    }

	quick = 1;
  }

  goods.quick    = quick;
  goods.spec     = spec_arr;
  goods.goods_id = goodsId;
  goods.number   = number;
  goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);

  //Ajax.call('flow.php?step=add_to_cart', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
  Ajax.call('flow.php?step=add_to_cart', 'goods=' + $.toJSON(goods), addToCartSilentResponse, 'POST', 'JSON');
  //alert('addToCart done');
}

/*
 * sshi:
 * 首先使用标准的addToCart方法，将已经选定规格的主商品加入购物车
 * 		其次通过循环，将被选定的套餐商品作为从商品加入购物车(只有商品id) 
 * */
function addPackToCart(goodsId, parentId){
	var goods        = new Object();
	var spec_arr     = new Array();
	var fittings_arr = new Array();
	var number       = 1;
	var formBuy      = document.forms['ECS_FORMBUY'];
	var quick		   = 0;
	
	var item_price = 0;
	
	// 检查是否有商品规格 
	if (formBuy)
	{
		spec_arr = getSelectedAttributes(formBuy);
	
	if (formBuy.elements['command_quantity'])
	{
		number = formBuy.elements['command_quantity'].value;
	}
		quick = 1;
	}
	
	goods.quick    = quick;
	goods.spec     = spec_arr;
	goods.goods_id = goodsId;
	goods.number   = number;
	goods.parent   = (typeof(parentId) == "undefined") ? 0 : parseInt(parentId);
	
	//alert('准备添加');
	//Ajax.call('flow.php?step=add_to_cart', 'goods=' + goods.toJSONString(), addToCartResponse, 'POST', 'JSON');
	//Ajax.call('flow.php?step=add_to_cart', 'goods=' + $.toJSON(goods), addToCartNoResponse, 'POST', 'JSON');
	
	//alert('开始加入其他套餐商品');
	// 添加其他套餐商品进入购物车	列表储存在	pack_goods_id_list
	var pack_goods_id_list = document.getElementById('pack_goods_id_list').value;
	if( pack_goods_id_list.length >0){
		var id_list=pack_goods_id_list.split('_');
		for(var i=0;i<id_list.length;i++){
			//		id_list[i] 是套餐商品id
			//alert(id_list[i]);
			if(isNaN(id_list[i])){       // 有非数字的商品id出现，不操作，跳转到购物车，返回，不进行后续操作
				//Ajax.call('flow.php');
				alert('addPackToCart error');
				return;
			}
		}
		//alert('套餐商品添加  before ');
		//Ajax.call('flow.php?step=add_pack_to_cart', 'parentId=' + goodsId + '&goods=' + pack_goods_id_list, addToCartSilentResponse, 'POST', 'TEXT');
		Ajax.call('flow.php?step=add_pack_to_cart', 'parentId=' + goodsId + '&goods=' + pack_goods_id_list, addToCartSilentResponse, 'POST', 'JSON');
		//alert('套餐商品添加  end ');
		//alert('套餐商品添加成功');		
	}

}


function addToCartNoResponse(){
	alert('no response');
	return;
}




function addToCartSilentResponse(result){
	//alert('套餐商品添加  addToCartSilentResponse ' + result.cart_goods_list + ' =================================================');
	if (result.error > 0)
	{
		$("#tips_big_div").show();
		$("#tips_word").html("添加失败。");
	}
	else
	{
		//var res = $.evalJSON(result);
		//alert('show cart '+ result.cart_goods_list);
		//$("#ECS_CART").html(result.cart_goods_list);
		document.getElementById('ECS_CART').innerHTML = result.cart_goods_list;
		
		$("[name=shopping_Car]").show();
		$("[name=shopping_Car]").delay(2000).hide(0); 
	}
}

/**
 * 获得选定的商品属性
 */
function getSelectedAttributes(formBuy)
{
  var spec_arr = new Array();
  var j = 0;

  for (i = 0; i < formBuy.elements.length; i ++ )
  {
    var prefix = formBuy.elements[i].name.substr(0, 5);

    if (prefix == 'spec_' && (
      ((formBuy.elements[i].type == 'radio' || formBuy.elements[i].type == 'checkbox') && formBuy.elements[i].checked) ||
      formBuy.elements[i].tagName == 'SELECT'))
    {
      spec_arr[j] = formBuy.elements[i].value;
      j++ ;
    }
  }

  return spec_arr;
}

